using SimpleUnitity.Cache;
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;

namespace SimpleUnitity.Web
{
    [Serializable]
    public sealed class WarpSession
    {
        static readonly string SessionKey = string.Format("__SIMPLEUNTITY_WEB_WARPSESSION_RAMDOMKEY_{0}__", new Random().Next(0, int.MaxValue));

        private WarpSession() { }

        public static WarpSession Current
        {
            get
            {
                if (HttpContext.Current == null || HttpContext.Current.Session == null)
                {
                    return null;
                }
                WarpSession session = HttpContext.Current.Session[SessionKey] as WarpSession;
                if (session == null)
                {
                    HttpContext.Current.Session[SessionKey] = session = new WarpSession();
                }
                return session;
            }
        }

        public HttpSessionState HttpSession
        {
            get
            {
                if (HttpContext.Current == null || HttpContext.Current.Session == null)
                {
                    return null;
                }
                return HttpContext.Current.Session;
            }
        }

        public bool IsAuthenticated
        {
            get { return string.IsNullOrEmpty(username) == false; }
        }

        public object UserKey { get; set; }

        private string username = string.Empty;
        public string UserName
        {
            get { return username; }
        }

        public void LogOn(string username)
        {
            this.username = username;
        }

        public void LogOff()
        {
            this.username = string.Empty;
        }

        public void LogOff(bool clearSession)
        {
            this.username = string.Empty;
            if (clearSession && HttpSession != null)
            {
                HttpSession.Clear();
            }
        }

        public void RemoveData(string key)
        {
            Cache.Remove(key);
        }

        public void SetData(string key, object value)
        {
            Cache.Set(key, value);
        }

        public object GetData(string key, object defaultValue = null)
        {
            return Cache.Get(key, defaultValue);
        }

        public ICache Cache
        {
            get { return SessionCache.Current; }
        }

        private static readonly string msgQueueKey = string.Format("__SimpleUnitity_Web_Messages_RANDOM_{0}__", new Random().NextDouble());
        public Queue Messages
        {
            get
            {
                Queue queue = Cache.Get(msgQueueKey) as Queue;
                if (queue == null)
                {
                    queue = new Queue(new NormalCache());
                    Cache.Set(msgQueueKey, queue);
                }
                return queue;
            }
        }
        
    }
}
